layui.config({
  base: '../static/js/' //假设这是你存放拓展模块的根目录
})
layui.extend({
  admin: 'admin'
});
layui.use(['form', 'layer', 'admin'], function () {
  var form = layui.form,
    admin = layui.admin,
    layer = layui.layer;
  form.render();
  //自定义验证规则
  form.verify({
    nikename: function (value) {
      if (value.length < 5) {
        return '昵称至少得5个字符啊';
      }
    }
    , pass: [/(.+){6,12}$/, '密码必须6到12位']
    , repass: function (value) {
      if ($('#L_pass').val() != $('#L_repass').val()) {
        return '两次密码不一致';
      }
    }
  });

  // 监听提交
  form.on('submit(add)', function (data) {
    console.log(data);
    //发异步，把数据提交ajax
    layer.alert("增加成功", { icon: 6 }, doAdd());
    return false;
  });

});



function doAdd(){
  //
  // 使用JQuery发送一个ajax请求
  var name = $('#username').val();
  var pwd = $("[name='password']").val();
  var email = $("[name='email']").val();
  var role = $("[name='role']").val();
  var sex = $("[name='sex']").val();
  var telephone =  $("[name='telephone']").val();
  var imgPath = $("#image")[0].src.substring(21);



  $.ajax({
    url:"/user/doadd",
    dataType:"json",
    type:"post",
    data:{
      "username" :name,"password":pwd,"email":email,"role":role,"sex":sex,"telephone":telephone,"pictureUrl":imgPath
    },
    success:function (data){
      if (data.flag){
        location.href='/user/userlist'
      }else {
        alert(data.msg);
        location.reload()
      }

    }
  })

}
